﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Collections.Generic;

public partial class Order_NewReturnDoc : System.Web.UI.Page
{
    private DataSet OrderDetail
    {
        get
        {
            DataSet orderDetail = null;
            if (ViewState["OrderDetail"] != null)
            {
                orderDetail = (DataSet)ViewState["OrderDetail"];
            }
            return orderDetail;
        }
        set
        {
            ViewState["OrderDetail"] = value;
        }
    }

    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        Check.CheckPermission("400102");
        if (!IsPostBack)
        {
            if (Request.QueryString["code"] != null)
            {
                TextBox1.Text = Request.QueryString["code"].ToString().Trim();
                
                GetData();

                GetOrderInfo();
            }
            GetDefaultData();
        }
    }


    private void GetDefaultData()
    {
        List<Model.RETURNTYPE> rtList = new BLL.RETURNTYPE().GetAllList();
        DropDownList2.DataSource = rtList;
        DropDownList2.DataTextField = "RT_NAME";
        DropDownList2.DataValueField = "RT_CODE";
        DropDownList2.DataBind();

        List<Model.USERS> uList=new BLL.USERS().GetList(String.Empty, " users_stop=0 ", " users_default desc ");
        DropDownList3.DataSource = uList;
        DropDownList3.DataTextField = "users_name";
        DropDownList3.DataValueField = "users_code";
        DropDownList3.DataBind();
    }

    #region 订单主信息
    private void GetOrderInfo()
    {
        string orderid = Request.QueryString["code"].ToString().Trim();
        Model.ORDERDOC od = new BLL.ORDERDOC().GetModel(orderid);


        Model.V_CUSTOMER customer = new BLL.V_CUSTOMER().GetModel(od.ORDER_CUSTOMERCODE);
        lblgmr.Text = customer.DM;
        lblRealName.Text = customer.GKMC;
        lblNickName.Text = customer.BYZD5;
        lblgmr.ToolTip = od.ORDER_CUSTOMERCODE;
        txtshr.Text = od.ORDER_GETER;
        txtshrdh.Text = od.ORDER_GETERPHONE1;
        txtshrdz.Text = od.ORDER_GETADDRESS;
        txtshrsj.Text = od.ORDER_GETERPHONE2;
        txtshryb.Text = od.ORDER_GETPOSTCODE;

        List<Model.TRANER> traner = new BLL.TRANER().GetAllList(true);
        ddlwlfs.DataSource = traner;
        ddlwlfs.DataTextField = "TRANER_NAME";
        ddlwlfs.DataValueField = "TRANER_CODE";
        ddlwlfs.DataBind();
        ddlwlfs.SelectedValue = od.ORDER_TRANERCODE;

        //ddlfyxz.SelectedValue = od.ORDER_COSTNATURE;
        Label2.Text = Decimal.Round(od.ORDER_AMT, 2).ToString();
        Label3.Text = Decimal.Round(od.ORDER_TAMT, 2).ToString();
        Label4.Text = od.ORDER_SECDEAL.ToString();


    }
    #endregion



    #region 订单商品明细
    private void GetData()
    {
        string orderid = Request.QueryString["code"].ToString().Trim();
        this.OrderDetail = new BLL.ORDERDETAIL().P_Order_GetProductMX(orderid);
        DataList1.DataSource = this.OrderDetail.Tables[0].DefaultView;
        DataList1.DataBind();

        hidOrderDetailCount.Value = this.OrderDetail.Tables[0].Rows.Count.ToString(); 

    }
    protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
    {
        if (e.CommandName == "delete")
        {
            this.OrderDetail.Tables[0].Rows.RemoveAt(e.Item.ItemIndex);
            DataList1.DataSource = this.OrderDetail;
            DataList1.DataBind();
            hidOrderDetailCount.Value = this.OrderDetail.Tables[0].Rows.Count.ToString();
        }
    }
    protected void DataList1_ItemDataBound(object sender, DataListItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
        {
            string spdm = ((Label)e.Item.FindControl("lblspdm")).Text.Trim();
            string color = ((HiddenField)e.Item.FindControl("hdcolor")).Value.Trim();
            string size = ((HiddenField)e.Item.FindControl("hdsize")).Value.Trim();
            DataSet gg1 = new BLL.SHANGPIN().GetSPGG1(spdm);
            DataSet gg2 = new BLL.SHANGPIN().GetSPGG2(spdm);
            DropDownList ddlcolor = (DropDownList)e.Item.FindControl("color");
            DropDownList ddlsize = (DropDownList)e.Item.FindControl("size");
            ddlcolor.DataSource = gg1.Tables[0].DefaultView;
            ddlcolor.DataBind();
            ddlcolor.SelectedValue = color;
            ddlsize.DataSource = gg2.Tables[0].DefaultView;
            ddlsize.DataBind();
            ddlsize.SelectedValue = size;
        }

    }
    #endregion


    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            if (!Check.CheckPermission("400102", "00001"))
            {
                Share.Alert(Page, "无权操作"); return;
            }
            bool bj = false;
            decimal zsl = 0;
            decimal je = 0.00M;
            decimal zje = 0.00M;
            decimal zjf = 0.00M;
            List<Model.RETURNDOCDETAIL> rddlist = new List<Model.RETURNDOCDETAIL>();
            for (int g = 0; g < DataList1.Items.Count; g++)
            {
                if (DataList1.Items[g].Enabled)
                {
                    Model.RETURNDOCDETAIL rdd = new Model.RETURNDOCDETAIL();
                    string spdm = ((Label)DataList1.Items[g].FindControl("lblspdm")).Text.Trim();
                    string color = ((DropDownList)DataList1.Items[g].FindControl("color")).SelectedValue;
                    string size = ((DropDownList)DataList1.Items[g].FindControl("size")).SelectedValue;
                    string sl = ((TextBox)DataList1.Items[g].FindControl("txtQuantity")).Text.Trim();
                    string oldsl = ((TextBox)DataList1.Items[g].FindControl("txtOldQuantity")).Text.Trim();
                    string zk = ((Label)DataList1.Items[g].FindControl("lblDiscount")).Text.Trim();
                    string price = ((Label)DataList1.Items[g].FindControl("lblSprice")).Text.Trim();
                    string OD_SPPRICE = ((Label)DataList1.Items[g].FindControl("lblOD_SPPRICE")).Text.Trim();
                    string OD_ITGBUY = ((Label)DataList1.Items[g].FindControl("lblOD_ITGBUY")).Text.Trim();
                    string OD_ITGPRICE = ((Label)DataList1.Items[g].FindControl("lblOD_ITGPRICE")).Text.Trim();
                    string OD_ITG = ((Label)DataList1.Items[g].FindControl("lblOD_ITG")).Text.Trim();
                    string OD_ADDITG = ((Label)DataList1.Items[g].FindControl("lblOD_ADDITG")).Text.Trim();
                    string weight = ((Label)DataList1.Items[g].FindControl("lblweight")).Text.Trim();
                    rdd.RDD_CMDCODE = spdm;
                    rdd.RDD_DETAILID = g.ToString();
                    rdd.RDD_COLORCODE = color;
                    rdd.RDD_SIZECODE = size;
                    rdd.RDD_QTY = Int32.Parse(sl);
                    rdd.RDD_DISC = Decimal.Parse(zk);
                    rdd.RDD_SPRICE = Decimal.Parse(price);
                    rdd.RDD_APRICE = rdd.RDD_SPRICE * rdd.RDD_DISC;
                    rdd.RDD_SAMT = rdd.RDD_QTY * rdd.RDD_SPRICE;
                    rdd.RDD_AAMT = rdd.RDD_APRICE * rdd.RDD_QTY;
                    rdd.RDD_SPPRICE = Int32.Parse(OD_SPPRICE);
                    rdd.RDD_ITGBUY = Int32.Parse(OD_ITGBUY);
                    rdd.RDD_ITGPRICE = OD_ITGPRICE == "" ? 0.00M : Decimal.Parse(OD_ITGPRICE);
                    rdd.RDD_FIELD2 = System.Configuration.ConfigurationSettings.AppSettings["kwdm"].ToString();
                    rdd.RDD_RETURNITG = ((OD_ITG == "" ? 0.00M : Decimal.Parse(OD_ITG)) / Int32.Parse(oldsl) - (OD_ADDITG == "" ? 0.00M : Decimal.Parse(OD_ADDITG)) / Int32.Parse(oldsl)) * rdd.RDD_QTY;
                    rdd.RDD_WEIGHT = Decimal.Parse(weight == "" ? "0" : weight);
                    rddlist.Add(rdd);
                    bj = true;
                    zsl += rdd.RDD_QTY;
                    je += rdd.RDD_AAMT;
                    zjf += rdd.RDD_RETURNITG;
                }
            }
            if (TextBox1.Text.Trim() == "" || lblgmr.ToolTip.Trim() == "" || txtshr.Text.Trim() == "")
            {
                Share.Alert(Page, "请填写*号必要信息！"); return;
            }
            if (bj)
            {
                Model.RETURNDOC rd = new Model.RETURNDOC();
                rd.RD_CUSTOMERCODE = lblgmr.ToolTip;
                rd.RD_DOCID = TextBox1.Text.Trim();
                rd.RD_TRANER = ddlwlfs.SelectedValue;
                rd.RD_TRANCOST = txtwlfy.Text.Trim() == "" ? 0.00M : Decimal.Parse(txtwlfy.Text.Trim());
                rd.RD_OFFERAMT = txtRangLi.Text.Trim() == "" ? 0.00M : Decimal.Parse(txtRangLi.Text.Trim());
                //rd.RD_COSTTYPE = ddlfyxz.SelectedValue;
                rd.RD_TRANID = txtwldh.Text.Trim();
                rd.RD_RETURNER = txtshr.Text;
                rd.RD_RETURNPHONE1 = txtshrdh.Text;
                rd.RD_RETURNERPHONE2 = txtshrsj.Text;
                rd.RD_RETURNADDRESS = txtshrdz.Text;
                rd.RD_RETURNPOSTCODE = txtshryb.Text;
                rd.RD_QTY = zsl;
                rd.RD_AMT = je;
                rd.RD_AAMT = je - rd.RD_TRANCOST+rd.RD_OFFERAMT;
                rd.RD_CREATER = Share.UserValue(0);
                rd.RD_CREATEDATE = DateTime.Now;
                rd.RD_NOTE = txtbz.Text;
                rd.RD_SUBITG = zjf;
                //rd.RD_RETURNTYPE = DropDownList1.SelectedValue;
                rd.RD_SECDEAL = Int32.Parse(Label4.Text.Trim());
                rd.RD_REASON = DropDownList2.SelectedValue;
                rd.RD_DUTY = DropDownList3.SelectedValue;
                string va = new BLL.RETURNDOCDETAIL().ReturnDoc_Insert(rd, rddlist);
                if (va == null || va.Trim() == "")
                {
                    Share.Alert(Page, "失败！");
                }
                else
                {
                    Response.Redirect("ReturnDoc.aspx");
                }
            }
            else
            {
                Share.Alert(Page, "没有要退的商品！");
            }
        }
        catch (FormatException ex)
        {
            Share.Alert(Page, "您输入的数值有误，请输入正确的数值！");
        }
    }

}
